package com.ipad.utils;

import com.google.gson.Gson;

import java.util.concurrent.TimeUnit;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;

/**
 * Created by sev-14 on 2017/4/12.
 */

public class OkHttpUtils {
    public static final String APP_SECRET = "846335d9d06f20b4f8e6489066fd5c2e";
    //    public static String URL_BASE = "http://192.168.0.113/?";
//    public static String URL_BASE = "http://h.api.gewmd.com/?";
//
    public static String URL_BASE = "https://api.heyimed.com/?";
    public static String APP_KEY = "wx2cbc232f2178b677";
    public static String VERSION = "1";
    private static OkHttpClient mClient;
    private static Gson gson;

    public static OkHttpClient getOkHttpClientInstance() {
        if (mClient == null) {
            mClient = new OkHttpClient();
            OkHttpClient.Builder builder = mClient.newBuilder();
            builder.connectTimeout(15, TimeUnit.SECONDS);
            builder.readTimeout(15, TimeUnit.SECONDS);
            builder.writeTimeout(15, TimeUnit.SECONDS);
            mClient = builder.build();
        }
        return mClient;
    }

    public static Gson getGsonInstance() {
        if (gson == null) {
            gson = new Gson();
        }
        return gson;
    }

    //获取Request
    public static Request getRequest(String method, String data) {
        FormBody.Builder builder = new FormBody.Builder();
        builder.add("method", method);
        builder.add("app_key", APP_KEY);
        builder.add("v", VERSION);
        builder.add("sign", MD5Utils.getMD5Str(data + APP_SECRET));
        builder.add("data", data);
        Request request = new Request.Builder().post(builder.build()).build();
        return request;
    }
}
